<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-06-06 17:42:19
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-06-08 15:57:11
 * @FilePath: \vue-admin-template\src\views\news\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <!-- 搜索框 -->
    <div class="search">
      <el-input v-model="searchForm.title" style="width:200px" placeholder="搜索文章" />
      <el-button @click="search">搜索</el-button>
      <el-button @click="clear">清空</el-button>
    </div>
    <el-button @click="$router.push('/news/add')">新增页面</el-button>
    <!-- 表格主体 -->
    <el-table border style="width: 100%" :data="newList">
      <el-table-column label="id" width="180">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="carousel" label="标题" width="400">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column prop="carousel" label="图片" width="400">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="" width="300px">
        </template>
      </el-table-column>
      <el-table-column prop="date" label="日期">
        <template slot-scope="scope">
          最后修改日期: {{ scope.row.updatedAt }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="toEdit(scope.row._id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="remove(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="pagination.total"
      :page-size="pagination.pageSize"
      :current-page.sync="pagination.pageNumber"
      @current-change="iniNew"
    />
  </div>
</template>

<script>
import { getNewList, delNew } from '@/api/news'
export default {
  data() {
    return {
      newList: [],
      searchForm: {
        content: '',
        title: ''
      },
      pagination: {
        pageNumber: 1,
        pageSize: 3,
        total: 1
      }
    }
  },
  created() {
    this.iniNew()
  },
  methods: {
    async iniNew(searchForm = {}) {
      const { pageSize, pageNumber } = this.pagination
      const res = await getNewList({ pageSize, pageNumber, ...searchForm })
      this.newList = res.data
      this.pagination = res.pagination
      console.log(res)
    },
    search() {
      this.iniNew(this.searchForm)
    },
    clear() {
      this.searchForm.title = ''
      this.iniNew()
    },
    toEdit(val) {
      this.$router.push(`/news/edit?id=${val}`)
    },
    async remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delNew(id)
        this.iniNew()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
